package cn.bestick.cblog.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bestick.cblog.dto.ArticleDto;
import cn.bestick.cblog.dto.ArticleLiteDto;
import cn.bestick.cblog.dto.MsgDto;
import cn.bestick.cblog.service.ArticleService;
import cn.bestick.cblog.service.MsgService;
import cn.bestick.cblog.service.impl.ArticleServiceImpl;
import cn.bestick.cblog.service.impl.MsgServiceImpl;

public class ArticleServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ArticleService articleService = new ArticleServiceImpl();
	private MsgService msgService = new MsgServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");

		String articleId = request.getParameter("articleId");
		ArticleDto article = articleService.getArticle(Integer
				.parseInt(articleId));
		/*
		 * Article updateArticle = article;
		 * updateArticle.setClicks(updateArticle.getClicks() + 1);
		 * articleService.update(updateArticle);
		 */
		ArticleLiteDto preArticle = articleService.getPreArticle(Integer
				.parseInt(articleId));
		ArticleLiteDto nextArticle = articleService.getNextArticle(Integer
				.parseInt(articleId));
		List<MsgDto> comments = msgService.getComments(article.getId());
		request.setAttribute("type", 0);
		request.setAttribute("replytype", 2);
		request.setAttribute("articleId", articleId);
		request.setAttribute("pid", articleId);
		request.setAttribute("msgs", comments);
		request.setAttribute("article", article);
		request.setAttribute("preArticle", preArticle);
		request.setAttribute("nextArticle", nextArticle);
		request.setAttribute("mainPage", "template/article/detail.jsp");
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}
}
